/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.admin.controller.versions;

import cn.hutool.http.server.HttpServerRequest;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shop.cereshop.admin.service.versions.AppVersionsService;
import com.shop.cereshop.admin.service.versions.CereAgreementService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.domain.versions.AppVersions;
import com.shop.cereshop.commons.domain.versions.CereAgreement;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


/**
 * <p>
 * 前端控制器
 * 多端应用版本管理表
 * </p>
 *
 * @author
 * @date 2025-08-02
 */
@Slf4j
@RestController
@RequestMapping("/appVersions")
@Api(value = "AppVersions", tags = "多端应用版本管理表")
public class AppVersionsController {
    @Autowired
    private AppVersionsService appVersionsService;

    @Autowired
    private CereAgreementService cereAgreementService;

    /**
     * 根据id查询 协议表
     *
     * @return 协议内容
     */
    @GetMapping("getById")
    @ApiOperation(value = "分页查询协议内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "版本类型", value = "verSionType", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "协议id", value = "ContentId", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "当前页码", value = "pageNum", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "每页数量", value = "pageSize", required = false, dataType = "Long", paramType = "query")

    })
    public Result<Page<CereAgreement>> getById(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) String verSionType, @RequestParam(required = false) Long ContentId) {
        /*
          1 用户协议
          2 隐私政策
          3 招住类型
          4 合作流程
         */
        return new Result(cereAgreementService.getPage(pageNum, pageSize, verSionType, ContentId), CoReturnFormat.SUCCESS);
    }


    //    新增协议内容
    @PostMapping("addAgreement")
    @ApiOperation(value = "新增协议内容")
    public Result addAgreement(@RequestBody CereAgreement cereAgreement, HttpServletRequest request) {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        cereAgreement.setCreatorId(user.getPlatformUserId());

        boolean save = cereAgreementService.save(cereAgreement);
        if (!save) {
            return new Result(CoReturnFormat.FAIL);
        }
        return new Result(CoReturnFormat.SUCCESS);
    }

    //修改协议内容
    @PostMapping("updateAgreement")
    @ApiOperation(value = "修改协议内容")
    public Result updateAgreement(@RequestBody CereAgreement cereAgreement) {
        boolean b = cereAgreementService.updateById(cereAgreement);
        if (!b) {
            return new Result(CoReturnFormat.FAIL);
        }
        return new Result(CoReturnFormat.SUCCESS);
    }

    //    根据id删除协议内容·
    @DeleteMapping("/del/{contentId}")
    @ApiOperation(value = "根据id删除协议内容")
    public Result deleteAgreement(@PathVariable Long contentId) {
        boolean b = cereAgreementService.removeById(contentId);
        if (!b) {
            return new Result(CoReturnFormat.FAIL);
        }
        return new Result(CoReturnFormat.SUCCESS);
    }


    @GetMapping("getPage")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "页码", value = "pageNum", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "每页数量", value = "pageSize", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "版本类型", value = "verSionType", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, dataType = "Long", paramType = "query")
    })
    public Result<IPage<AppVersions>> getPage(Integer pageNum, Integer pageSize, String verSionType, Long appId) {
        return new Result(appVersionsService.getPage(pageNum, pageSize, verSionType, appId), CoReturnFormat.SUCCESS);
    }

    @PostMapping("add")
    @ApiOperation(value = "保存")
    public Result save(@RequestBody AppVersions agreement) {
        return new Result(CoReturnFormat.SUCCESS, appVersionsService.insertAppInfo(agreement));
    }

    @PostMapping("update")
    @ApiOperation(value = "更新")
    public Result update(@RequestBody AppVersions appVersions) {
        // 处理 releaseTime 为空对象的情况
        if (appVersions.getReleaseTime() == null) {
            appVersions.setReleaseTime(LocalDateTime.now());
        }
        Integer number = appVersionsService.updateAppinfo(appVersions);
        if (number > 0) {

            return new Result(CoReturnFormat.SUCCESS);

        }
        return new Result(CoReturnFormat.FAIL);

    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "主键id", value = "id", required = true, dataType = "Long", paramType = "path")
    })
    public Result delete(@PathVariable Long id) {
        return new Result(CoReturnFormat.SUCCESS, appVersionsService.removeById(id));
    }


}
